package com.atxwq.shop.manager.serivce.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.atxwq.shop.common.exception.GlobalException;
import com.atxwq.shop.manager.mapper.SysMenuMapper;
import com.atxwq.shop.manager.mapper.SysRoleMenuMapper;
import com.atxwq.shop.manager.serivce.SysMenuService;
import com.atxwq.shop.manager.utils.MenuHelper;
import com.atxwq.shop.model.entity.system.SysMenu;
import com.atxwq.shop.model.entity.system.SysUser;
import com.atxwq.shop.model.vo.common.ResultCodeEnum;
import com.atxwq.shop.model.vo.system.SysMenuVo;
import com.atxwq.utils.AuthContextUtil;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @Description TODO
 * @Date 2025/5/12 16:15
 * @Created by 12336
 */

@Service
@Slf4j
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper  sysRoleMenuMapper;

    /**
     * 查询菜单列表
     * @return
     */
    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> list = sysMenuMapper.findNodes();
        //如果菜单列表为空直接返回null
        if(CollectionUtil.isEmpty(list)) {
            return null;
        }
        //如果菜单列表不为空，则进行菜单树形构建
        List<SysMenu> treelist = MenuHelper.buildTree(list);
        return treelist;
    }

    /**
     * 添加菜单功能
     * @param sysMenu
     */
    @Override
    public void save(SysMenu sysMenu) {
       sysMenuMapper.insert(sysMenu);

       //新添加了菜单，就要把父级菜单的设置为半开状态
        updateSysRoleMenuIsHalf(sysMenu) ;
    }

    //修改父菜单状态
    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {
        //获取父菜单状态
        SysMenu  parentMenu = sysMenuMapper.selectParentMenu(sysMenu.getParentId());
        if(parentMenu != null){
            // 将该id的菜单设置为半开
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId()) ;
            //递归调用
            updateSysRoleMenuIsHalf(parentMenu);
        }

    }

    /**
     * 修改菜单功能
     * @param sysMenu
     */
    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
    }

    /**
     * 删除菜单功能
     * @param id
     */
    @Override
    public void removeById(Long id) {
        //1.先查询是否存在子菜单
        int count = sysMenuMapper.countByParentId(id);
        if(count > 0){
            throw new GlobalException(ResultCodeEnum.NODE_ERROR);
        }
        sysMenuMapper.removeById(id);
    }

    /**
     * 查询用户菜单动态回显
     * @return
     */
    @Override
    public List<SysMenuVo> findMenuList() {
        //1.获取当前用户信息
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();
        //2.构建树形数据
        List<SysMenu> list = sysMenuMapper.selectListByUserId(userId);
        List<SysMenu> sysMenuList = MenuHelper.buildTree(list);
        return this.buildMenus(sysMenuList);
    }

    //将SysMenu构建成SysMenuVo
    private List<SysMenuVo> buildMenus(List<SysMenu> sysMenuList) {
        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : sysMenuList) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        log.info("菜单数据：{}", sysMenuVoList);
        return sysMenuVoList;
    }

}
